<template>
  <div class="new-container">
    <div class="item" v-for="item in sortedList" :key="item.id" @click="$router.push({ path: '/detial', query: { id: item.id } })">
      <div class="header">
        <img :src="item.avatar" alt="" />
        <div>
          <div class="title">{{ item.stem }}</div>
          <div class="meta">
            <span>{{ item.creator }}</span>
            <span>|</span>
            <span>{{ item.createdAt }}</span>
          </div>
        </div>
      </div>
      <div class="middle" v-html="item.content"></div>
      <div class="bottom">
        <span>点赞 {{ item.likeCount }}</span>
        <span>|</span>
        <span>浏览 {{ item.views }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'HmNew',
  data() {
    return {
      list: [],
      sorter: 'weight_desc', // 默认按 weight_desc 排序
    };
  },
  created() {
    this.getList();
  },
  computed: {
    sortedList() {
      return this.list.slice().sort((a, b) => {
        if (a[this.sorter] < b[this.sorter]) return 1;
        if (a[this.sorter] > b[this.sorter]) return -1;
        return 0;
      });
    },
  },
  methods: {
    async getList() {
      try {
        const res = await axios({
          url: 'http://interview-api-t.itheima.net/h5/interview/query',
          method: 'get',
          params: {
            sorter: this.sorter, // 传递 sorter 参数
          },
        });
        console.log(res);
        this.list = this.list.concat(res.data.data.rows);
      } catch (err) {
        console.log(err);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.new-container {
  padding: 50px 10px 0;
}

.item {
  padding: 10px;
  border: 1px solid #ddd;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  border-radius: 8px;
}

.header {
  display: flex;
  align-items: center;

  img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
  }

  .title {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 5px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }

  .meta {
    font-size: 14px;
    color: #666;
  }
}

.middle {
  font-size: 16px;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.bottom {
  font-size: 14px;
  color: #999;
}
</style>